//
//  PLDirectory.h
//  Plankton
//
//  Created by Marc Schlichte on 2008-07-13.
//  Copyright 2008-2009 Framework Labs. All rights reserved.
//

#import <Foundation/Foundation.h>

#import "PLInteractor.h" // for interactor-context

@class PLPlatform, PLPattern;

@protocol PLDirectoryDelegate;

#if MAC_OS_X_VERSION_MAX_ALLOWED < MAC_OS_X_VERSION_10_6 || TARGET_OS_IPHONE

@protocol NSNetServiceDelegate @end
@protocol NSNetServiceBrowserDelegate @end

#endif

/*!
    @class
    @abstract    A simple Directory Service to look up registered action-ids.
    @discussion  This directory service associates action-ids with service-names
                which can be looked up afterwards. Instances of this class can be
                given the action-ids of peer directory services which are then taken
                into account when doing the lookup.                
*/
@interface PLDirectory : NSObject <PLInteractorContext, NSNetServiceDelegate, NSNetServiceBrowserDelegate>
{
    PLPlatform              *platform;
    NSString                *dirAid;
    id<PLDirectoryDelegate> delegate;
    
    NSMutableDictionary     *serviceToAidsDict;
    NSMutableSet            *peers;
    
    PLMulticastRequestor    *aidsRequestor;
    NSString                *serviceToSearch;
    PLResponder             *aidsResponder;
    
    PLMulticastRequestor    *servicesRequestor;
    NSMutableSet            *foundServices;
    PLResponder             *servicesResponder;
    
    BOOL                    discoverable;
    BOOL                    autoDiscover;
    NSNetService            *ownService;
    NSNetServiceBrowser     *serviceBrowser;
    
    BOOL                    receiveUpdates;
    NSMutableSet            *peersToPush;
    PLPattern               *pushRegistrationPattern;
    PLPattern               *pushPattern;
}

- (id)initWithPlatform:(PLPlatform *)platform;

@property(nonatomic, assign) id<PLDirectoryDelegate> delegate;

@property(nonatomic, readonly) NSString *aid;

- (void)registerAid:(NSString *)aid forService:(NSString *)service;
- (void)unregisterAid:(NSString *)aid;

- (void)searchAidsForService:(NSString *)service;
- (void)searchServices;

- (BOOL)isSearchingForAids;

- (void)addPeer:(NSString *)aid;
- (void)removePeer:(NSString *)aid;

@property BOOL discoverable;	//!< set this to true to be visible by other directories
@property BOOL autoDiscover;	//!< set this to true to automatically discover peer directories around 

@property BOOL receiveUpdates;	//!< set this to true if this directory should update if peer directories change

@end


/// The protocol directory delegate can implement to get notified about found services and actors.
///
@protocol PLDirectoryDelegate <NSObject>
@optional
- (void)directory:(PLDirectory *)directory foundAid:(NSString *)aid forService:(NSString *)service;
- (void)directory:(PLDirectory *)directory searchDoneForService:(NSString *)service;

- (void)directory:(PLDirectory *)directory foundServices:(NSSet *)services;

- (void)directoryChangedPeerSet:(PLDirectory *)directory;

- (void)directory:(PLDirectory *)directory addedAid:(NSString *)aid forService:(NSString *)service;
- (void)directory:(PLDirectory *)directory removedAid:(NSString *)aid forService:(NSString *)service;

@end
